<?php
/**
 * Most Read Top10排行榜
 * @使用地方：频道页MostRead 明星库明星动态 频道页快速阅读
 */
namespace haibao\cms\business\control\pc\channel;
use haibao\cms\business\control\ArticleController;

class MoreReadController extends ChannelController{
	

	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		
		$this->replaceAdPosition($controlData['auto'],true);
		
		if($this->checkData($controlData)){
			$this->getMostReadData( $controlData['manual']);
			
			$path = $this->generateHtmlPath(__CLASS__);
			$isNewHtml = $this->generateHtml($path,$content);
			$content = $this->getHtml($path);
			$content = $this->replaceCDNLink($content);
		}else{
		    if(isset($controlData['manual']['limit']) && $controlData['manual']['limit']==4 && isset($controlData['manual']['topTagId']) && $controlData['manual']['topTagId']==310){
		        $this->getFastReadDataByChannel($controlData['manual']);
		        $path = $this->generateHtmlPath(__CLASS__);
		        $isNewHtml = $this->generateHtml($path,$content);
		        $content = $this->getHtml($path);
		        $content = $this->replaceCDNLink($content);
		    }else{
		        return $content = '<div></div>';
		    }
		}
		return $content;
	}
	
	
	/**
	 * 调用拥有该?标签，前?天浏览量最高，?级及以上的?条资讯和图集
	 */
	public function getMostReadData($dataCondition){
		$dataCondition['source'] = __CLASS__;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
		$dataCondition['isDate'] = true;
		//$dataCondition['isPrimaryTag'] = true;
		$dataCondition['isSwitchUrl'] = isset($dataCondition['isSwitchUrl'])?$dataCondition['isSwitchUrl']:0;
		$this->parseTagId($dataCondition['topTagId']);
		list($articles,$imageColls) = $this->getArticleControl(2,$dataCondition);
 		$mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
		$mostReadData = array();
		if($dataCondition['isSwitchUrl']==1){
		    foreach($mergeData as $data){
		        $temp = array();
		        if($data->SiteType == ArticleController::INFO_TYPE_ARTICLE){
		            if(isset($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN])){
		                $temp['ArticleProImage'] = self::getImageLink($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN],400,400);
		            }else{
		                if(isset($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
		                    $temp['ArticleProImage'] = self::getImageLink($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO],400,400);
		                }else{
		                    $temp['ArticleProImage'] = '';
		                }
		            }
		            $temp['TagName'] = $articles['tag'][$data->ArticleTag->TagId]['TagName'];
		            $temp['TagUrl'] = $articles['tag'][$data->ArticleTag->TagId]['Url'];
		            $temp['Channel'] = self::getArticleUrl($data->PublishId,$data->ArticleTag->ParentId);
		        }else{
		            if(isset($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN])){
		                $temp['ArticleProImage'] = self::getImageLink($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN],400,400);
		            }else{
		                if(isset($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
		                    $temp['ArticleProImage'] = self::getImageLink($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO],400,400);
		                }else{
		                    $temp['ArticleProImage'] = '';
		                }
		            }
		            $temp['TagName'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['TagName'];
		            $temp['TagUrl'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['Url'];
		            $temp['Channel'] = self::getArticleUrl($data->Id,$data->ImageCollectiveTag->ParentId,ArticleController::INFO_TYPE_IMAGE);
		        }
		    
		        $temp['Id'] = $data->Id;
		        $temp['Title'] = $data->Title;
		        $temp['ShortTitle'] = $data->ShortTitle?$data->ShortTitle:$data->Title;
		        $temp['SiteType'] = $data->SiteType;
		        array_push($mostReadData,$temp);
		    }
		} else {
		    foreach($mergeData as $data){
		        $temp = array();
		        if($data->SiteType == ArticleController::INFO_TYPE_ARTICLE){
		            if(isset($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN])){
		                $temp['ArticleProImage'] = $articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN];
		            }else{
		                if(isset($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
		                    $temp['ArticleProImage'] = $articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
		                }else{
		                    $temp['ArticleProImage'] = '';
		                }
		            }
		            $temp['TagName'] = $articles['tag'][$data->ArticleTag->TagId]['TagName'];
		            $temp['TagUrl'] = $articles['tag'][$data->ArticleTag->TagId]['Url'];
		            $temp['Channel'] = self::getArticleUrl($data->PublishId,$data->ArticleTag->ParentId);
		        }else{
		            if(isset($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN])){
		                $temp['ArticleProImage'] = $imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_SEVEN];
		            }else{
		                if(isset($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
		                    $temp['ArticleProImage'] = $imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
		                }else{
		                    $temp['ArticleProImage'] = '';
		                }
		            }
		            $temp['TagName'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['TagName'];
		            $temp['TagUrl'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['Url'];
		            $temp['Channel'] = self::getArticleUrl($data->Id,$data->ImageCollectiveTag->ParentId,ArticleController::INFO_TYPE_IMAGE);
		        }
		    
		        $temp['Id'] = $data->Id;
		        $temp['Title'] = $data->Title;
		        $temp['ShortTitle'] = $data->ShortTitle?$data->ShortTitle:$data->Title;
		        $temp['SiteType'] = $data->SiteType;
		        array_push($mostReadData,$temp);
		    }
		}

		$this->assign('mostReadData',$mostReadData);
	}
	
	public function getFastReadDataByChannel($dataCondition){
	    $dataCondition['source'] = __CLASS__;
	    $dataCondition['isTag'] = true;
	    $dataCondition['isConditionTag'] = true;
	    $dataCondition['isPrimaryTag'] = true;
	    $dataCondition['isImageColl'] = true;
		$dataCondition['isProImage'] = false;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['isParentTag'] = true;
		$this->parseTagId($dataCondition['topTagId']);
		
		if($this->conditionTagId == 310){
		    $dataCondition['level'] = 1;
		    $dataCondition['biglevel'] = 2;
		}else{
		    $dataCondition['level'] = 1;
		    $dataCondition['biglevel'] = 2;
		}
		list($articles,$imageColls) = $this->getArticleControl(1,$dataCondition);
		$mergeData = $this->mergeArticleImageColls($articles['list'],$imageColls['list'],4);
		$mostReadData = array();
		foreach($mergeData as $data){
		    $temp = array();
		    if($data->SiteType == ArticleController::INFO_TYPE_ARTICLE){
		        $temp['TagName'] = $articles['tag'][$data->ArticleTag->TagId]['TagName'];
		        $temp['TagUrl'] = $articles['tag'][$data->ArticleTag->TagId]['Url'];
		        $temp['Channel'] = self::getArticleUrl($data->PublishId,$this->conditionTagId);
		    }else{
		        $temp['TagName'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['TagName'];
				$temp['TagUrl'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['Url'];
				$temp['Channel'] = self::getArticleUrl($data->Id,$data->ImageCollectiveTag->ParentId,ArticleController::INFO_TYPE_IMAGE);
		    }
		    $temp['Id'] = $data->Id;
		    $temp['Title'] = $data->Title;
		    $temp['ShortTitle'] = $data->ShortTitle?$data->ShortTitle:$data->Title;
		    $temp['SiteType'] = $data->SiteType;
		    array_push($mostReadData,$temp);
		}
		$this->assign('mostReadData',$mostReadData);
	}
	
	private function mergeArticleImageColls($articles,$imageColls,$count=4){
	    $mergeArr = array();
	    if($imageColls){
	        $i = 0;
	        foreach($articles as $key=>$article){
	            if($i<3){
	               $mergeArr[$key] = $article;
	            }
	            $i++;
	        }
	        $mergeArr[$key] = array_shift($imageColls);
	    }else{
	        $mergeArr = array_slice($articles, 0, 4);
	    }
	    $mergeArr = array_values($mergeArr);
	    return $mergeArr;
	}
	
	private function checkData($controlData){
		
		if($controlData['manual']){
			
			if(isset($controlData['manual']['limit']) &&
			isset($controlData['manual']['level']) &&
			isset($controlData['manual']['topTagId']) &&
			isset($controlData['manual']['dateType']) &&
			isset($controlData['manual']['queryType']) 
			
			){
				
				return true;
				
			}else{
				
				return false;
			}
		}
	}
	
}